package com.xiaoa.aop;

import java.io.File;
import java.lang.reflect.Method;
import java.util.LinkedHashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.alibaba.fastjson.JSON;
import com.xiaoa.annotation.JsonCache;
import com.xiaoa.base.Response;
import com.xiaoa.utils.ExceptionUtils;

/**
 * log
 * @author xiaoa
 * @date 2018年11月15日 下午9:18:32
 * @version V1.0
 *
 */
@Component
@Aspect
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class LogAspect {
	
	@Autowired(required = false)
	HttpServletRequest request;
	
	@Autowired(required = false)
	HttpServletResponse response;
	
	public boolean check(){
		if(request == null || response == null ){
			return false;
		}
		
		return true;
	}

	
    @Around("execution("
    		+ "@org.springframework.web.bind.annotation.RequestMapping  "
    		+ "@org.springframework.web.bind.annotation.ResponseBody "
    		+ " * *(..))")
	public Object controllLog(ProceedingJoinPoint  join ) throws Throwable{
    	
    	if(!check()){
    		return pass(join);
    	}
    	
    	try {
    		return pass(join);
		} finally{
			System.out.println("log_out:controllLog");
		}
    	
    	
    
    }
	
    
    /**
     * tong guo 
     * @Title: pass
     * @param join
     * @return
     * @throws Throwable
     * @author xiaoa
     */
    public Object pass(ProceedingJoinPoint  join)throws Throwable{
		return join.proceed(join.getArgs());
    }
    
    
	
    @Around("within(com.xiaoa..*)")
	public Object log(ProceedingJoinPoint  join ) throws Throwable{
    	
    	if(!check()){
    		return pass(join);
    	}
    	
    	// 当前执行方法
    	Method method = null;
    	
    	// 获取方法参数
    	Object[]  args = join.getArgs();
    	
    	// 方法参数名
    	String[]  parameterNames = null;
    	
    	// 日志map
    	LinkedHashMap<String, Object>  logMap =  new LinkedHashMap<>();
    	
    	// 是否回滚事务
    	boolean isRollback = false;
    	
    	// 如果是方法
    	if (join.getSignature()  instanceof MethodSignature){
    		
    		// 获取方法签名
    		MethodSignature methodSignature = (MethodSignature)join.getSignature();
    		
    		// 获取方法名
    		parameterNames = methodSignature.getParameterNames();
    		
    		// 获取方法
    		method =  methodSignature.getMethod();
    		
    		// 获取方法类型
    		Class<?>[]  argsClass =  method.getParameterTypes();
    		
    		
    	}
    
    	logMap.put("method", method.getName());
        System.out.println(JSON.toJSONString(logMap));
        
        long startTime = System.currentTimeMillis();

    
    	try {
    		// 执行用户方法
    		return pass(join);

		} finally {
			response.setHeader("use", System.currentTimeMillis() - startTime + "");
			// 写出日志
	    	if (!logMap.isEmpty()){
	    		 logMap.put("class",join.getTarget().getClass().getSimpleName() + "." + method.getName());
	    		 logMap.put("use", System.currentTimeMillis() - startTime);
	    	     System.out.println(JSON.toJSONString(logMap));
	    	}
	    	
		}
    
    }


}
